package com.sesame.service;

import com.sesame.bean.Column;
import com.sesame.bean.DbInfo;
import com.sesame.bean.Table;
import com.sesame.util.TableUtil;
import kim.sesame.framework.utils.StringUtil;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * 数据库处理,父方法
 * 
 * @author wangjianghai (johnny_hzz@qq.com)
 * @date 2016年10月15日 下午4:27:27
 * @Description:
 */
public abstract class DBService {

	static Logger log = Logger.getLogger(DBService.class);
	// ----------------------------------------------------------
	// -------------------- 定义公用的抽象函数
	// ------------------------------------------------------------

	/** 获取这个数据库中的所有表名称 */
	public abstract List<Table> getTables(Connection conn);

	/** 解析表结构 */
	public abstract List<Column> parseTable(String tableName, Connection conn);

	/** 匹配数据类型 */
	public static void init(List<Column> list) {
		String str = "";
		for (Column c : list) {

			c.setJavaName(TableUtil.hump(c.getName(), false));
			c.setComment(c.getComment().trim());
			// -------------------------------------------------
			if (c.getType().contains("(")) {
				str = c.getType().substring(0, c.getType().indexOf('('));
			} else {
				str = c.getType();
			}
			if (StringUtil.equals(str, "int") || StringUtil.equals(str, "integer")) {
				c.setJavaType("Integer");
			} else if (StringUtil.equals(str, "long")) {
				c.setJavaType("Long");
			} else if (StringUtil.equals(str, new String[] { "float", "double" })) {
				c.setJavaType("double");
			} else if (StringUtil.equals(str, new String[] { "number", "decimal", "numeric", "real" })) {
				c.setJavaType("BigDecimal");
			} else if (StringUtil.equals(str, new String[] { "varchar", "varchar2", "char", "nvarchar", "nchar", "text" })) {
				c.setJavaType("String");
			} else if (StringUtil.equals(str, new String[] { "datetime", "date", "timestamp" })) {
				c.setJavaType("Date");
			} else {
				c.setJavaType("String");
				System.err.println("没有匹配到这个类型javatype:" + str);
			}
			// --------------------------------------------------------------------------------------------
			if (StringUtil.equals(str, "int") || StringUtil.equals(str, "integer")) {
				c.setJdbcType("INTEGER");
			} else if (StringUtil.equals(str, "long")) {
				c.setJdbcType("LONGVARBINARY");
			} else if (StringUtil.equals(str, new String[] { "float", "double" })) {
				c.setJdbcType("DOUBLE");
			} else if (StringUtil.equals(str, new String[] { "number", "decimal", "numeric", "real" })) {
				c.setJdbcType("DOUBLE");
			} else if (StringUtil.equals(str, new String[] { "varchar", "varchar2", "nvarchar" })) {
				c.setJdbcType("VARCHAR");
			} else if (StringUtil.equals(str, new String[] { "char", "nchar" })) {
				c.setJdbcType("CHAR");
			} else if (StringUtil.equals(str, new String[] { "datetime", "date", "timestamp" })) {
				c.setJdbcType("DATE");
			} else {
				c.setJdbcType("VARCHAR");
				System.err.println("没有匹配到这个类型jdbctype:" + str);
			}

			System.out.println(c.toString());
		}

	}

	/**
	 * 获取数据库连接 @author wangjianghai (johnny_hzz@qq.com)
	 * 
	 * @date 2016年10月15日 下午4:27:38 @throws
	 */
	public static Connection getConn(DbInfo bean) {
		try {
			Class.forName(bean.getDb_driver());
			return DriverManager.getConnection(bean.getDb_url(), bean.getDb_name(), bean.getDb_pwd());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库连接失败");
			log.info("url:" + bean.getDb_url());
			log.info("name:" + bean.getDb_name());
			log.info("pwd:" + bean.getDb_pwd());
			return null;
		}
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @author wangjianghai (johnny_hzz@qq.com)
	 * @date 2016年10月15日 下午4:27:54
	 */
	public static void closeConn(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
